Skip to content
This repository has been archived by the owner on Jan 2, 2021. It is now read-only.

added partial support for -p flags in lsbom #207

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

brtsai
Copy link
Contributor

@brtsai brtsai commented Dec 5, 2016

No description provided.

* Username
* UserGroupName
*/
std::string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: static std::string

* UserGroupName
*/
std::string
printItemToString(Options::PrintItem item, std::string path, struct bom_path_info_2 *info){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: PrintItemToString (or StringFromPrintItem)
nit: { on a new line

*/
std::string
printItemToString(Options::PrintItem item, std::string path, struct bom_path_info_2 *info){

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove empty line

return std::to_string(ntohl(info->group));
case Options::PrintItem::GroupName:

break;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For these unfinished ones, I'd do it like this:

case Options::PrintItem::SomeCase:
    return std::string(); // TODO: implement

break;
}

return "";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Once the unimplemented ones have return above, can remove this.)

@@ -451,6 +513,18 @@ main(int argc, char **argv)
*/
if (options->onlyPath()) {
printf("%s\n", path.c_str());
} else if (options->printFormat()) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove empty line

std::string toPrint;

for (Options::PrintItem item : *options->printFormat()) {
toPrint += first? "" : "\t";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: space before ?

@brtsai
Copy link
Contributor Author

brtsai commented Dec 5, 2016

screen shot 2016-12-05 at 4 19 09 pm

toReturn.push_back(Options::PrintItem::FileSize);
toReturn.push_back(Options::PrintItem::Checksum);

return toReturn;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could use an "initializer list" to create the vector:

std::vector<Options::PrintItem> toReturn = {
    Options::PrintItem::FileName,
    Options::PrintItem::Permissions,
    Options::PrintItem::UserGroupID,
    Options::PrintItem::FileSize,
    Options::PrintItem::Checksum,
};

return toReturn;

Or even remove the temporary variable completely:

return {
    Options::PrintItem::FileName,
    Options::PrintItem::Permissions,
    Options::PrintItem::UserGroupID,
    Options::PrintItem::FileSize,
    Options::PrintItem::Checksum,
};

I like that style better for two reasons. Most importantly, I think it's easier to read: the structure of the code matches the structure of the data structure created. And not important in this case, but it's also slightly faster since it avoids calling push_back five times, each of which might need to expand the vector's allocated space.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The initialiser list looks like a slightly faster, cleaner way to do this.
I'll make that change, thanks!

}

printf("\n");
toPrint = PrintItemsToString(GetDefaultPrintFormat(), path, path_info_2_value);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than the else if and else, here, you could do this:

} else {
    std::vector<Options::PrintItem> printFormat = options->printFormat().value_or(GetDefaultPrintFormat());
    toPrint = PrintItemsToString(printFormat, path, path_info_2_value);
}

Using the built-in value_or function to use a default if no printFormat is specified in the options.

Beyond that, you could even remove the if entirely:

std::vector<Options::PrintItem> printFormat = options->printFormat().value_or(
    options->onlyPath() ? { Options::PrintItem::FileName  } : GetDefaultPrintFormat());
toPrint = PrintItemsToString(printFormat, path, path_info_2_value);

Then, you could remove the PrintItemsToString function and just put the code right in here, since it's only called once.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I notice that in your value_or solution, if printFormat has been filled in via the -p flag, then onlyPath no longer takes precedence over the -p flag as lsbom.
Perhaps you meant:
std::vector<Options::PrintItem> printFormat = options->onlyPath() ? std::vector<Options::PrintItem>({Options::PrintItem::FileName}) : options->printFormat().value_or(GetDefaultPrintFormat());

And this too looks good!
I'll get this in there, and thanks again for the feedback!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, sounds best to ignore the second half of my comment!

}

static std::vector<Options::PrintItem>
GetDefaultPrintFormat ()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: no space before ()

refactored code for printing lsbom details
@brtsai
Copy link
Contributor Author

brtsai commented Jan 29, 2017

Tried pulling a fresh copy from the current main version, and noticed at lsbom doesn't seem to work anymore as I get "error: failed to load paths tree."

Tried tracing it to see what went wrong and here's what I got so far:

bom.c::bom_variable_iterate returned get_context.data_index == -1

	before bom.c::bom_variable_get returned -1

		before bom_tree.c::bom_tree_alloc_load returned null

			before lsbom:: under comment Create Bom Tree for paths errored with “failed to load paths tree error”

@grp
Copy link
Contributor

grp commented Jan 31, 2017

@brtsai I think #225 will fix that

@brtsai
Copy link
Contributor Author

brtsai commented Feb 6, 2017

Thanks!
225 did it!
I've merged the latest ver into my repo, added my changes and pushed.
All of the Travis tests seem to have passed and all of the changes posted in your review should be implemented.
Let me know if there's anything I've missed.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants